#import "YYText.h"
#import "dfhgwhruebvhrheycontaminategeneralize.h"
#import "GKCover.h"
#import "dfhgwhruebvhrheyspotfaint.h"
#import "dfhgwhruebvhrheyoutwarddelegate.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyincidentallycomet.h"
#import "dfhgwhruebvhrheytransmissionairplane.h"
#import "dfhgwhruebvhrheybangorganismViewController.h"
#import "dfhgwhruebvhrheychoruspea.h"
#import "dfhgwhruebvhrheyrevolutionarycapsule.h"
#import "dfhgwhruebvhrheyassemblepursuitViewController.h"
#import "dfhgwhruebvhrheyconnectionencounterViewController.h"
@interface dfhgwhruebvhrheyassemblepursuitViewController ()
@property (nonatomic, strong) UIButton *agreeBtn;
@property (nonatomic, strong) dfhgwhruebvhrheyincidentallycomet *infoModel;
@end
@implementation dfhgwhruebvhrheyassemblepursuitViewController  {
	UIView *topView;
	UILabel *amountLabel;
	UILabel *amount;
	UILabel *termLabel;
	UILabel *term;
	UIView *infoView;
	UILabel *issuedLabel;
	UILabel *issued;
	UILabel *serviceLabel;
	UIButton *iconBtn;
	UILabel *service;
	UILabel *planLabel;
	dfhgwhruebvhrheycontaminategeneralize *line;
	UIView *termView;
	YYLabel *protocol;
	UIButton *applyBtn;
}
- (UILabel *)generateLabelWithText:(NSString *)text withTextColor:(UIColor *)color {
	UILabel *lb = [[UILabel alloc] init];
	lb.font = [UIFont medium:17];
	lb.textColor = color;
	lb.text = text;
	return lb;
}
- (void)viewDidLoad {
	[super viewDidLoad];
	[self loadUI];
	[self loadPlanInfo];
}
- (void)loadUI {
	[self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
	[self configRightIcon:@"navigation_right"];
	self.backgroundColor = [UIColor whiteColor];
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
	self.navigationItem.title = @"Confirm loan information";
	topView = [[UIView alloc] init];
	topView.layer.cornerRadius = 10;
	topView.layer.borderWidth = 0.5;
	topView.layer.borderColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate.CGColor;
	[self.containerView addSubview:topView];
	amountLabel = [self generateLabelWithText:@"Loan Amount" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	amountLabel.font = [UIFont regular:15];
	[topView addSubview:amountLabel];
	amount = [self generateLabelWithText:@"" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	amount.font = [UIFont regular:15];
	[topView addSubview:amount];
	termLabel = [self generateLabelWithText:@"Loan Term" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	termLabel.font = [UIFont regular:15];
	[topView addSubview:termLabel];
	term = [self generateLabelWithText:@"" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	term.font = [UIFont regular:15];
	[topView addSubview:term];
	infoView = [[UIView alloc] init];
	infoView.backgroundColor = @"#FAFAFA".dhgfywegwygeydwlearneddesignate;
	infoView.layer.cornerRadius = 10;
	[topView addSubview:infoView];
	issuedLabel = [self generateLabelWithText:@"Issued Amount" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	issuedLabel.font = [UIFont regular:14];
	[infoView addSubview:issuedLabel];
	issued = [self generateLabelWithText:@"" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	issued.font = [UIFont regular:14];
	[infoView addSubview:issued];
	serviceLabel = [self generateLabelWithText:@"Service Fees" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	serviceLabel.textAlignment = NSTextAlignmentLeft;
	serviceLabel.font = [UIFont regular:14];
	[infoView addSubview:serviceLabel];
	iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[iconBtn setImage:[UIImage imageNamed:@"ppp_service_icon"] forState:UIControlStateNormal];
	[infoView addSubview:iconBtn];
	[iconBtn addTarget:self action:@selector(serviceFeesClick) forControlEvents:UIControlEventTouchUpInside];
	service = [self generateLabelWithText:@"" withTextColor:@"#3B414B".dhgfywegwygeydwlearneddesignate];
	service.font = [UIFont regular:14];
	[infoView addSubview:service];
	planLabel = [self generateLabelWithText:@"Repayment Plan" withTextColor:primary_color.dhgfywegwygeydwlearneddesignate];
	planLabel.font = [UIFont medium:16];
	[self.containerView addSubview:planLabel];
	line = [[dfhgwhruebvhrheycontaminategeneralize alloc] init];
	line.backgroundColor = [UIColor clearColor];
	[self.containerView addSubview:line];
	termView = [[UIView alloc] init];
	[self.containerView addSubview:termView];
	self.agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	self.agreeBtn.tag = 100;
	[self.agreeBtn setImage:[UIImage imageNamed:@"ppp_login_agree_n"] forState:UIControlStateNormal];
	[self.agreeBtn setImage:[UIImage imageNamed:@"ppp_login_agree_s"] forState:UIControlStateSelected];
	[self.containerView addSubview:self.agreeBtn];
	[self.agreeBtn addTarget:self action:@selector(loanPlanBtnAction:) forControlEvents:UIControlEventTouchUpInside];
	__weak typeof(self) weakSelf = self;
	NSString *protocolString = @"I have read and understand the \"Loan Agreement\"";
	NSRange range = [protocolString rangeOfString:@"\"Loan Agreement\""];
	NSMutableAttributedString *attriute = [[NSMutableAttributedString alloc] initWithString:protocolString];
	attriute.yy_color = @"#3B414B".dhgfywegwygeydwlearneddesignate;
	attriute.yy_font = [UIFont regular:13];
	[attriute yy_setTextHighlightRange:range color:primary_color.dhgfywegwygeydwlearneddesignate backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf protocolClick];
	}];
	protocol = [[YYLabel alloc] init];
	protocol.attributedText = attriute;
	[self.containerView addSubview:protocol];
	applyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	applyBtn.tag = 200;
	applyBtn.layer.cornerRadius = 10;
	applyBtn.titleLabel.font = [UIFont semiBold:18];
	applyBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
	[applyBtn setTitle:@"Apply for withdrawal" forState:UIControlStateNormal];
	[applyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[applyBtn addTarget:self action:@selector(loanPlanBtnAction:) forControlEvents:UIControlEventTouchUpInside];
	[self.containerView addSubview:applyBtn];
}
- (void)protocolClick {
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheybangorganismViewController *webVC = [[dfhgwhruebvhrheybangorganismViewController alloc] init];
	webVC.isShowBottomView = YES;
	NSString *urlString = [NSString stringWithFormat:@"%@?money=%@&use_days=%@&token=%@",lendProtocalH5, self.selectedAmount.amount, self.seletedTerm.days, [dfhgwhruebvhrheyoptionsaving getUserData].token];
	[webVC loadURLString:urlString];
	webVC.navigationItem.title = @"Loan Service Agreement";
	[self.navigationController pushViewController:webVC animated:YES];
	[webVC bottomBtnsClick:^(UIButton * _Nonnull sender) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.agreeBtn.selected = YES;
	}];
}
- (void)serviceFeesClick {
	dfhgwhruebvhrheychoruspea *popView = [[dfhgwhruebvhrheychoruspea alloc] initWithFrame:CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-26, 285)];
	[GKCover coverFrom:dfhgwhruebvhrheyoptionsaving.keyWindow contentView:popView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:YES];
	[popView bindData:self.infoModel andClick:^{
		[GKCover hideCover];
	}];
}
- (void)loadPlanInfo {
	NSMutableDictionary *params = [NSMutableDictionary dictionary];
	params[@"rate_id"] = self.selectedAmount.rid;
	params[@"price"] = self.selectedAmount.amount;
	params[@"days"] = self.seletedTerm.days;
	params[@"pid"] = self.seletedTerm.info.p_id;
	params[@"tid"] = self.seletedTerm.ud_id;
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:loanPlanDataApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.infoModel = [dfhgwhruebvhrheyincidentallycomet yy_modelWithJSON:json];
		if (strongSelf.infoModel.code == 200) {
			[strongSelf bindTermView];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:strongSelf.infoModel.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)bindTermView {
	if(self.infoModel) {
		NSString *amountString = [NSString stringWithFormat:@"%d",self.infoModel.loan_amount].dhgfywegwygeydwlocalitylearned;
		amount.text = [NSString stringWithFormat:@"PHP %@", amountString];
		term.text = [NSString stringWithFormat:@"%d Days", self.infoModel.loan_term];
		NSString *issuedString = [NSString stringWithFormat:@"%d",self.infoModel.issued_amount].dhgfywegwygeydwlocalitylearned;
		issued.text = [NSString stringWithFormat:@"PHP %@", issuedString];
		NSString *serviceString = [NSString stringWithFormat:@"%d",self.infoModel.service_fee].dhgfywegwygeydwlocalitylearned;
		service.text = [NSString stringWithFormat:@"PHP %@", serviceString];
		if(self.infoModel.repay_data.count == 1) {
			dfhgwhruebvhrheyrevolutionarycapsule *singleView = [[dfhgwhruebvhrheyrevolutionarycapsule alloc] init];
			singleView.layer.cornerRadius = 10;
			singleView.layer.borderWidth = 0.5;
			singleView.layer.borderColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate.CGColor;
			[termView addSubview:singleView];
			[singleView bindData:self.infoModel.repay_data.firstObject];
			[singleView mas_makeConstraints:^(MASConstraintMaker *make) {
				make.edges.equalTo(termView);
			}];
		}else {
			dfhgwhruebvhrheytransmissionairplane *tmpMultiView = nil;
			for (int i = 0; i < [self.seletedTerm.info.instalment_num intValue]; i ++) {
				dfhgwhruebvhrheytransmissionairplane *multiView = [[dfhgwhruebvhrheytransmissionairplane alloc] init];
				PPP_LoanPlanRepayModel *repay = self.infoModel.repay_data[i];
				[multiView bindData:repay];
				multiView.layer.cornerRadius = 10;
				multiView.layer.borderWidth = 0.5;
				if(i == 0) {
					multiView.title = @"First installment";
				}else {
					multiView.title = @"Second installment";
				}
				multiView.layer.borderColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate.CGColor;
				[termView addSubview:multiView];
				[multiView mas_makeConstraints:^(MASConstraintMaker *make) {
					make.left.right.equalTo(termView);
					if(tmpMultiView == nil) {
						make.top.equalTo(termView);
					}else {
						make.top.equalTo(tmpMultiView.mas_bottom).offset(10).priorityHigh();
						if(i == [self.seletedTerm.info.instalment_num intValue] - 1) {
							make.bottom.equalTo(termView);
						}
					}
				}];
				tmpMultiView = multiView;
			}
		}
	}
}
- (void)loanPlanBtnAction:(UIButton *)sender {
	if (self.infoModel == nil) {
		[self loadPlanInfo];
	}else {
		if (sender.tag == 100) {
			sender.selected = !sender.selected;
		}else {
			if (!self.agreeBtn.isSelected) {
				return [self.view show:@"Please read and agree to the loan agreement"];
			}
			dfhgwhruebvhrheyconnectionencounterViewController *withdrawalVC = [[dfhgwhruebvhrheyconnectionencounterViewController alloc] init];
			withdrawalVC.seletedTerm = self.seletedTerm;
			withdrawalVC.selectedAmount = self.selectedAmount;
			[self.navigationController pushViewController:withdrawalVC animated:YES];
		}
	}
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	[topView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.containerView).offset(15);
		make.left.equalTo(self.containerView).offset(13);
		make.centerX.equalTo(self.containerView);
		make.height.mas_equalTo(166);
	}];
	[amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(topView).offset(15);
		make.left.equalTo(topView).offset(10);
		make.height.mas_equalTo(21);
	}];
	[amount mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(topView).offset(-10);
		make.centerY.equalTo(amountLabel);
		make.height.mas_equalTo(21);
	}];
	[termLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(amountLabel.mas_bottom).offset(10);
		make.left.equalTo(amountLabel);
		make.height.mas_equalTo(21);
	}];
	[term mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(termLabel);
		make.right.equalTo(amount);
		make.height.mas_equalTo(21);
	}];
	[infoView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.height.mas_equalTo(74);
		make.left.equalTo(termLabel);
		make.centerX.equalTo(topView);
		make.top.equalTo(termLabel.mas_bottom).offset(10);
	}];
	[issuedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(infoView).offset(12);
		make.left.equalTo(infoView).offset(10);
		make.height.mas_equalTo(20);
	}];
	[issued mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(issuedLabel);
		make.right.equalTo(infoView).offset(-10);
		make.height.mas_equalTo(20);
	}];
	[serviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(issuedLabel.mas_bottom).offset(10);
		make.size.mas_equalTo(CGSizeMake(90, 20));
		make.left.equalTo(issuedLabel);
	}];
	[iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(serviceLabel.mas_right);
		make.size.mas_equalTo(CGSizeMake(15, 15));
		make.centerY.equalTo(serviceLabel);
	}];
	[service mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(serviceLabel);
		make.right.equalTo(issued);
		make.height.mas_equalTo(20);
	}];
	[planLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(topView.mas_bottom).offset(15);
		make.left.equalTo(topView);
		make.size.mas_equalTo(CGSizeMake(133, 23));
	}];
	[line mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(planLabel.mas_right);
		make.centerY.equalTo(planLabel);
		make.right.equalTo(topView);
		make.height.mas_equalTo(0.5);
	}];
	[line drawDashLine:@"#E6E6E6".dhgfywegwygeydwlearneddesignate];
	[termView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(planLabel);
		make.right.equalTo(topView);
		make.top.equalTo(planLabel.mas_bottom).offset(10);
	}];
	[self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(termView.mas_bottom).offset(20);
		make.left.equalTo(termView);
		make.size.mas_equalTo(CGSizeMake(20, 20));
	}];
	[protocol mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.agreeBtn.mas_right).offset(10);
		make.top.equalTo(self.agreeBtn);
		make.right.equalTo(termView);
		make.height.mas_equalTo(19);
	}];
	[applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.agreeBtn.mas_bottom).offset(21);
		make.centerX.equalTo(self.containerView);
		make.left.equalTo(self.agreeBtn);
		make.height.mas_equalTo(50);
		make.bottom.equalTo(self.containerView).offset(-20);
	}];
}
@end
